﻿package Leophy.text {
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.text.TextField;
	import Leophy.Interfaces.ISay;

	import Leophy.Interfaces.ILanguage;
	import Leophy.LeophyVersion;
	import Leophy.Net.XMLLoader;
	import Leophy.utils.Debug;

	/**
	 * ...
	 * @author TOPHY
	 */
	public class Language extends EventDispatcher implements ILanguage,ISay {

		private static var _instance : Language;

		/**
		 * Language 单例
		 */
		public static function get Ins() : Language {
			if (_instance == null) {
				_instance = new Language();
			}
			return _instance;
		}

		///---------------------------------------------------------   变量
		private var _Langs : Object;
		private var _langNames : Array;
		private var _initXMLUrl : String;
		private var _xmlloader : XMLLoader;
		private var _defaultLanguage : String = "";
		private var _languageLoaded:Boolean = false;
		///---------------------------------------------------------   事件
		public static var NULL_TIP_STR : String = "无资源";
		public static var M_LANGUAGE_CHANGED : String = "onLanguageChanged";
		public static var M_LOADED_LANGUAGE : String = "onLanguageLoaded";

		///---------------------------------------------------------   属性
		public function get Languageloaded():Boolean {
			return _languageLoaded;
		}
		/**
		 * 当前语言版本名
		 */
		public function get CurrentLanguage() : String {
			return _defaultLanguage;
		}

		public function set CurrentLanguage(value : String) : void {
			setLanguage(value);
		}

		/**
		 * 返回所有可用语言版本
		 */
		public function get LanguageNames() : Array {
			return _langNames;
		}

		///---------------------------------------------------------   函数
		public function Language(initXMLurl : String = "") {
			LeophyVersion.version();
			ClearAll();
			_xmlloader = new XMLLoader();
			_xmlloader.addEventListener(XMLLoader.COMPLETE_XML, xmlloaded);
			_xmlloader.addEventListener(IOErrorEvent.IO_ERROR, xmlloaderror);
			_xmlloader.addEventListener(ProgressEvent.PROGRESS, xmlloading);
			
			loadLGResource(initXMLurl);
		}

		/**
		 * 加载新的语言
		 * @param	initXMLurl 语言资源文件(xml)位置
		 */
		public function loadLGResource(initXMLurl : String = "") : void {
			if (initXMLurl != "" && initXMLurl != null) {
				_xmlloader.loadXML(initXMLurl);
				_languageLoaded = false;
			}
		}

		/**
		 * 从语言资源中获取当前语言版本下，对应资源号的内容
		 * @param	SID 资源号
		 * @return 文本
		 */
		public function GetText(SID : String = "") : String {
			if (_defaultLanguage == "" || SID == "" || SID == null) return fixNULLorEMPTYString();
			if (_Langs[_defaultLanguage] == null) return fixNULLorEMPTYString();
			
			return fixNULLorEMPTYString(_Langs[_defaultLanguage][SID]);
		}

		/**
		 * 设置资源内容为指定的语言版本内容
		 * @param	tf 文本框 （TextField）
		 * @param	SID 资源ID号
		 */
		public function SetTextField(tf : TextField, SID : String) : void {
			var t : String = GetText(SID);
			if (t != NULL_TIP_STR) {
				tf.text = t;
			}
		}

		/**
		 * 清空所有语言版本
		 */
		public function ClearAll() : void {
			_Langs = new Object();
			_langNames = new Array();
			setLanguage();
		}

		/**
		 * 检查是否有指定的语言版本
		 * 
		 * @param	languageName 语言版本名
		 */
		public function HaveLanguage(languageName : String = "") : Boolean {
			return _Langs[languageName] != null;
		}

		/**
		 * 更新当前语言版本
		 * @param	languageName 语言类型
		 * @return
		 */
		private function setLanguage(languageName : String = "") : Boolean {
			if (this._defaultLanguage != languageName && languageName != null) {
				this._defaultLanguage = languageName;
				this.dispatchEvent(new Event(M_LANGUAGE_CHANGED));
				return true;
			}
			return false;
		}

		private function xmlloaderror(ev : IOErrorEvent) : void {
			this.dispatchEvent(ev);
		}

		private function xmlloading(ev : ProgressEvent) : void {
			this.dispatchEvent(ev);
		}

		private function xmlloaded(ev : Event) : void {
			
			progressxml(ev.target.data);
		}

		private function progressxml(xml : XML) : void {
			for (var i : uint = 0;i < xml.language.length(); i++) {
				
				var lgts : XMLList = xml.language[i].text;
				var lgn : String = xml.language[i].@name.toString();
				
				if (this._Langs[lgn] == null) {
					this._Langs[lgn] = new Object();
					_langNames.push(lgn);
				}
				
				var obj : Object = new Object();
				for (var t : uint = 0;t < lgts.length(); t++) {
					obj[lgts[t].@id.toString()] = lgts[t].toString();
				}
				this._Langs[lgn] = obj;
				if (this._defaultLanguage == "") {
					this._defaultLanguage = lgn;
				}
			}
			
			Debug.traceObj(this._Langs)
			say("language loaded!");
			_languageLoaded = true;
			this.dispatchEvent(new Event(M_LOADED_LANGUAGE));
			
			if (_langNames.length > 0 && _defaultLanguage == "") {
				CurrentLanguage = _defaultLanguage;
			}
		}

		private function fixNULLorEMPTYString(txt : String = "") : String {
			return ((txt == null || txt == "") ? NULL_TIP_STR : txt);
		}
		
		/* INTERFACE Leophy.Interfaces.ISay */
		
		public function say(o:*):void
		{
			trace("LANG:"+o.toString())
		}
	}
}